/**
 * ZhiZun.com Inc.
 * Copyright (c) 2004-2010 All Rights Reserved.
 */
package com.zhz.lib.project.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.controller.form.FeedbackSearchForm;
import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.ibatis.domain.feedbacklib.Feedbacklib;
import com.zhz.lib.project.service.feedback.FeedbackService;
import com.zhz.lib.project.vo.feedback.FeedbackVO;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzBeanUtils;
import com.zhz.project.common.util.zhzutil.ZhzCommonUtil;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * 
 * @author liwei2672
 * @version $Id: FeedbackController.java, v 0.1 2010-7-6 下午10:43:52 liwei2672
 *          Exp $
 */
@Controller
@RequestMapping("/feedback.do")
public class FeedbackController {
    private static Logger   logger   = Logger.getLogger(NewsController.class);

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private IbatisPage      ibatisPage;
    @Autowired
    private BaseDAO        baseDao;
 
    private static String   pageSql  = "selectAllFeedbacklibByPage";
    private static String   countSql = "selectAllFeedbacklibCount";

    @RequestMapping(method = RequestMethod.POST)
    public String getIbatisListPostNoAction(ModelMap model, FeedbackSearchForm feedbackSearchForm) {
        return page(model, feedbackSearchForm);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getIbatisListGetNoAction(ModelMap model, FeedbackSearchForm feedbackSearchForm) {
        return page(model, feedbackSearchForm);
    }

    @RequestMapping(method = RequestMethod.POST, params = "action=page")
    public String getIbatisListPost(ModelMap model, FeedbackSearchForm feedbackSearchForm) {
        return page(model, feedbackSearchForm);
    }

    @RequestMapping(method = RequestMethod.GET, params = "action=page")
    public String getIbatisListGet(ModelMap model, FeedbackSearchForm feedbackSearchForm) {
        return page(model, feedbackSearchForm);
    }

    private String page(ModelMap model, FeedbackSearchForm feedbackSearchForm) {
        ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(ZhzBeanUtils
            .describe(feedbackSearchForm), countSql, pageSql));

        // 将News转换为NewsVO
        List<Feedbacklib> feedbacklibList = (List<Feedbacklib>) model.get("list");
        List<FeedbackVO> feedbackVOList = new ArrayList<FeedbackVO>();
        if (feedbacklibList != null && feedbacklibList.size() > 0) {
            for (Feedbacklib feedbacklib : feedbacklibList) {
                FeedbackVO feedbackVO = new FeedbackVO();
                BeanUtils.copyProperties(feedbacklib, feedbackVO);
                Sortmap adminfrom = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId",feedbacklib.getAdminfrom());
                if(adminfrom!=null)
                    feedbackVO.setAdminFromString(adminfrom.getValue());
                feedbackVOList.add(feedbackVO);
            }
        }
        model.put("list", feedbackVOList);
        return "feedback";
    }

    @RequestMapping(method = RequestMethod.POST, params = "action=feedback")
    public String thanks(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        String Name = ZhzHttpRequestUtil.getParameters(request, "Name");
        String Email = ZhzHttpRequestUtil.getParameters(request, "Email");
        String inputRand = ZhzHttpRequestUtil.getParameters(request, "inputRand");
        String Comment = ZhzHttpRequestUtil.getParameters(request, "Comment");
        String rand = (String) request.getSession().getAttribute("rand");

        if (StringUtil.equals(inputRand, rand)) {
            com.zhz.lib.project.domain.feedback.Feedbacklib feedbacklib = new com.zhz.lib.project.domain.feedback.Feedbacklib();
            feedbacklib.setName(Name);
            feedbacklib.setEmail(Email);
            feedbacklib.setComments(Comment);
            feedbacklib.setCommentsdate(new Date());
            feedbacklib.setIp(ZhzCommonUtil.getIpAddr(request));
            feedbacklib.setAproveState(0);
            feedbacklib.setIfDelete(0);

            if (feedbackService.saveFeedback(feedbacklib)) {
                model.put("result", "添加记录成功!");
            } else {
                model.put("result", "添加记录失败!");
                model.put("Name", Name);
                model.put("Email", Email);
                model.put("Comment", Comment);
            }
        } else {
            model.put("result", "添加记录失败!您的验证码输入错误。");
            model.put("Name", Name);
            model.put("Email", Email);
            model.put("Comment", Comment);
        }

        return page(model, new FeedbackSearchForm());
    }

}
